/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oop.example.gui;

import com.oop.example.utils.Utils;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author -
 */
public class SettingsDialog extends javax.swing.JDialog {

    private SettingsTableModel model = null;

    public SettingsDialog(Window parent) {
        super(parent, ModalityType.APPLICATION_MODAL);
        initComponents();
        model = new SettingsTableModel();
        jTableSettings.setModel(model);
        jButtonSave.addActionListener(new SaveActionListener());
        jButtonCancel.addActionListener(new CancelActionListener());
        jButtonAdd.addActionListener(new AddActionListener());
        jButtonDel.addActionListener(new DeleteActionListener());
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableSettings = new javax.swing.JTable();
        jButtonSave = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jToolBar = new javax.swing.JToolBar();
        jButtonAdd = new javax.swing.JButton();
        jButtonDel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Настроечки");

        jScrollPane1.setViewportView(jTableSettings);

        jButtonSave.setText("Ok");

        jButtonCancel.setText("Отмена");

        jToolBar.setFloatable(false);
        jToolBar.setRollover(true);

        jButtonAdd.setText("Add");
        jButtonAdd.setToolTipText("Добавить");
        jButtonAdd.setFocusable(false);
        jButtonAdd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonAdd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar.add(jButtonAdd);

        jButtonDel.setText("Delete");
        jButtonDel.setFocusable(false);
        jButtonDel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonDel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar.add(jButtonDel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(209, Short.MAX_VALUE)
                .addComponent(jButtonCancel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonSave)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSave)
                    .addComponent(jButtonCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonDel;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableSettings;
    private javax.swing.JToolBar jToolBar;
    // End of variables declaration//GEN-END:variables

    private class SaveActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            Properties config = new Properties();
            for (int i = 0; i < model.keys.size(); i++) {
                config.setProperty(model.keys.get(i), model.values.get(i));
            }
            Utils.setConfig(config);
            SettingsDialog.this.setVisible(false);

        }
    }

    private class CancelActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            SettingsDialog.this.setVisible(false);
        }
    }

    private class AddActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            model.add();
        }
    }

    private class DeleteActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            model.delete(jTableSettings.getSelectedRow());
        }
    }

    private class SettingsTableModel extends DefaultTableModel {

        private List<String> keys = new ArrayList<String>();
        private List<String> values = new ArrayList<String>();

        public SettingsTableModel() {
            super();
            Properties config = Utils.getConfig();
            for (String s : config.stringPropertyNames()) {
                keys.add(s);
                values.add(config.getProperty(s));
            }
        }

        public void delete(int rowIndex) {
            if (rowIndex >= 0 && keys.size() > rowIndex) {
                keys.remove(rowIndex);
                values.remove(rowIndex);
                fireTableRowsDeleted(rowIndex, rowIndex);
            }
        }

        public void add() {
            keys.add("newKey");
            values.add("newValue");
            fireTableRowsInserted(keys.size() - 1, keys.size() - 1);
        }

        @Override
        public int getRowCount() {
            if (keys == null) {
                keys = new ArrayList<String>();
            }
            return keys.size();
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return columnIndex == 0 ? "Настройка" : "Значение";
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return String.class;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return true;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return columnIndex == 0 ? keys.get(rowIndex) : values.get(rowIndex);
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                keys.set(rowIndex, aValue.toString());
            } else {
                values.set(rowIndex, aValue.toString());
            }
        }
    }
}
